﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Model.Result.QixingManage;
using YiSha.Enum.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-25 18:49
    /// 描 述：微信群聊服务类
    /// </summary>
    public class WXCorpGroupChatService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<WXCorpGroupChatEntity>> GetList(WXCorpGroupChatListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<WXCorpGroupChatDto>> GetPageList(WXCorpGroupChatListParam param, Pagination pagination)
        {
            //var expression = ListFilter(param);
            //var list= await this.BaseRepository().FindList(expression, pagination);
            //return list.ToList();

            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.*,b.config_id,b.auto_create_room,b.scene,b.room_base_id,b.room_base_name,b.remark,b.qr_code,b.lasttime FROM wxcorp_group_chat  a Left JOIN
            group_chat_join_way b ON a.chat_id=b.chat_id where 1=1 ");

            if (!param.name.IsEmpty()) 
            {
                strSql.Append(" AND a.name=@name ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@name",param.name));
            }
            if (param.type != -1)
            {
                strSql.Append(" AND a.type=@type ");
                parameter.Add(DbParameterExtension.CreateDbParameter("@type", param.type));
            }
            

            IEnumerable<WXCorpGroupChatDto> list = await this.BaseRepository().FindList<WXCorpGroupChatDto>(strSql.ToString(), parameter.ToArray(), pagination);

            foreach (var item in list)
            {
                if (!item.config_id.IsEmpty())
                {
                    item.scene_doc = item.scene.GetDescriptionByEnum<scene_status_enum>();

                    item.auto_create_room_doc = item.auto_create_room.GetDescriptionByEnum<auto_create_room_enum>();

                    item.status_doc = item.status.GetDescriptionByEnum<chat_status_enum>();
                }
                else
                {
                    item.status_doc = "待配置";
                    item.status = null;
                }
            }

            return list.ToList();
        }

        public async Task<WXCorpGroupChatEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<WXCorpGroupChatEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(WXCorpGroupChatEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<WXCorpGroupChatEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<WXCorpGroupChatEntity, bool>> ListFilter(WXCorpGroupChatListParam param)
        {
            var expression = LinqExtensions.True<WXCorpGroupChatEntity>();
            if (param != null)
            {
                if (!param.name.IsEmpty()) 
                {
                    expression = expression.And(t => t.name.Contains(param.name));
                }
            }
            return expression;
        }
        #endregion
    }
}
